package com.auto.learning.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.bean.SleepModelBean;

public class SleepModeTimeAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<SleepModelBean> list;
	private int selectedPosition = 2;
	
	public SleepModeTimeAdapter(Context context,ArrayList<SleepModelBean> list){
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	ViewHolder holder;

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.sleepmode_adapter, null);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_unit = (TextView) convertView.findViewById(R.id.tv_unit);
			holder.linear_item = (LinearLayout) convertView.findViewById(R.id.linear_item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_time.setText(list.get(position).getTime()+"");
		if (list.get(position).getType() == 1) {
			holder.tv_unit.setText("分钟");
		}else if (list.get(position).getType() == 2) {
			holder.tv_unit.setText("小时");
		}
		if (selectedPosition == position) {
			if (Build.VERSION.SDK_INT >= 16) {
				holder.linear_item.setBackground(context.getResources().getDrawable(R.drawable.text_sleep_mode_item_sharp_green));
			}else {
				holder.linear_item.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.text_sleep_mode_item_sharp_green));
			}
			holder.tv_time.setTextColor(context.getResources().getColor(R.color.black));
			holder.tv_unit.setTextColor(context.getResources().getColor(R.color.black));
		}else {
			holder.tv_time.setTextColor(context.getResources().getColor(R.color.text_background_sleep_text_color));
			holder.tv_unit.setTextColor(context.getResources().getColor(R.color.text_background_sleep_text_color));
			if (Build.VERSION.SDK_INT >= 16) {
				holder.linear_item.setBackground(context.getResources().getDrawable(R.drawable.text_sleep_mode_item_sharp));
			}else {
				holder.linear_item.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.text_sleep_mode_item_sharp));
			}
		}
		
		return convertView;
	}

	class ViewHolder {
		LinearLayout linear_item;
		TextView tv_time, tv_unit;
	}

	public int getSelectedPosition() {
		return selectedPosition;
	}

	public void setSelectedPosition(int selectedPosition) {
		this.selectedPosition = selectedPosition;
	}
}
